/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import EntityBeans.OrderLine;
import EntityBeans.OrderProduct;
import EntityBeans.Product;
import SessionBeans.OrderLineFacadeLocal;
import SessionBeans.OrderProductFacadeLocal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import obj.ItemCart;

/**
 *
 * @author Pierre
 */
@ManagedBean(name="controller_Cart" )
@SessionScoped
public class Controller_Cart {

    /**
     * Creates a new instance of Controller_Cart
     */
    @EJB
    private OrderLineFacadeLocal orderLineFacade;
    @EJB
    private OrderProductFacadeLocal oproductfacade;
    
    @ManagedProperty ("#{login}")
    private Login client;
    private String id;
    private String qte;
    private ArrayList<ItemCart> listItem;
    



            
            
    public Controller_Cart() {
       listItem = new ArrayList<ItemCart>();
    }
    
    public void AddToCart()
    {   int stat =0;
        System.out.println("Add Cart");
        try{
            int intid = Integer.parseInt(getId());
            int intqte = Integer.parseInt(getQte());
        
            //Check if Item exist in list
            for (ItemCart item: listItem)
            {
               if (intid == item.getProd().getId() )
               {
                   System.out.println("update");
                   stat = 1;
                   item.setQte(item.getQte()+intqte);
               }
            }
            if (stat == 0)
            {
                System.out.println("Add Panier");
                Controller_GetItem C_getitem;
                ELContext facesContext = FacesContext.getCurrentInstance().getELContext();
                C_getitem = (Controller_GetItem) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(facesContext, null , "controller_GetItem");
                Product prod = C_getitem.getItemByID(intid);
                System.out.println("Product");
                listItem.add(new ItemCart( prod, intqte));
            }
        }
        catch(NumberFormatException e)
                {
                     System.out.println("error add card\n"+e.toString());
                }
    }

   
    public String getTotal() {
        double vartmp = 0.00;

         for (ItemCart item: listItem)
        {
            vartmp += item.getProd().getPrice().doubleValue()*item.getQte();
        }
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(2); // la tu auras au plus 2 chiffres apres la virgule
        nf.setMinimumFractionDigits(2);  // maintenant tout tes nombres auront 2 chiffres après la virgule

        
        return "Total: "+ nf.format(vartmp) +" €";
        
    }
    
    public DataModel getListItem()
    {
       DataModel ListCard;   
       ListCard = new ListDataModel(listItem);
           return ListCard;
    }
    
        public int getNbItem() {
            int tmp =0;
            tmp += listItem.size();
        return tmp;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the qte
     */
    public String getQte() {
        return qte;
    }

    /**
     * @param qte the qte to set
     */
    public void setQte(String qte) {
        this.qte = qte;
    }
    
    public void supItem(ItemCart Item)
    {
        listItem.remove(Item);
    }
    
    public String validCart()
    {
        System.out.println("validCard");
        OrderProduct op = new OrderProduct();
        op.setClient(client.getCurrentUser());
        oproductfacade.create(op);

        OrderProduct newOrder = oproductfacade.findByID(oproductfacade.findLastOrder(getClient().getCurrentUser()));

        if(listItem.size()>0)
        {
              for (ItemCart item: listItem)
                {
                    OrderLine ol = new OrderLine();
                    ol.setQte(item.getQte());
                    ol.setProduct(item.getProd());
                    ol.setItemType("Film-Music");
                    ol.setPrice(item.getProd().getPrice());
                    ol.setOrderProduct(newOrder); 
                    orderLineFacade.create(ol);
                }
        }

        listItem.removeAll(listItem);
        return "OrderOk";      
    }

    /**
     * @return the client
     */
    public Login getClient() {
        return client;
    }

    /**
     * @param client the client to set
     */
    public void setClient(Login client) {
        this.client = client;
    }
}
